package hu.bme.mit.androtext.gen.resources;

import com.google.inject.Inject;
import hu.bme.mit.androtext.androgen.androGenDsl.TargetApplication;
import hu.bme.mit.androtext.androgui.androGuiDsl.BaseLayout;
import hu.bme.mit.androtext.androgui.androGuiDsl.Layout;
import hu.bme.mit.androtext.androgui.androGuiDsl.LayoutStyle;
import hu.bme.mit.androtext.androgui.androGuiDsl.PreferenceScreen;
import hu.bme.mit.androtext.androgui.androGuiDsl.RootLayout;
import hu.bme.mit.androtext.androgui.androGuiDsl.UIElement;
import hu.bme.mit.androtext.androgui.androGuiDsl.Widget;
import hu.bme.mit.androtext.gen.IGeneratorSlots;
import hu.bme.mit.androtext.gen.ILayoutResourceGenerator;
import hu.bme.mit.androtext.gen.util.GeneratorExtensions;
import java.util.Arrays;
import java.util.List;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.ResourceExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;

@SuppressWarnings("all")
public class LayoutResourceGenerator implements ILayoutResourceGenerator {
  @Inject
  private GeneratorExtensions extensions;
  
  public void doGenerate(final ResourceSet resourceSet, final IFileSystemAccess fsa, final TargetApplication androidApplication) {
    EList<Resource> _resources = resourceSet.getResources();
    final Function1<Resource,Iterable<RootLayout>> _function = new Function1<Resource,Iterable<RootLayout>>() {
        public Iterable<RootLayout> apply(final Resource r) {
          Iterable<EObject> _allContentsIterable = ResourceExtensions.allContentsIterable(r);
          Iterable<RootLayout> _filter = IterableExtensions.<RootLayout>filter(_allContentsIterable, hu.bme.mit.androtext.androgui.androGuiDsl.RootLayout.class);
          return _filter;
        }
      };
    List<Iterable<RootLayout>> _map = ListExtensions.<Resource, Iterable<RootLayout>>map(_resources, _function);
    Iterable<RootLayout> _flatten = IterableExtensions.<RootLayout>flatten(_map);
    for (final RootLayout layout : _flatten) {
      if ((layout instanceof PreferenceScreen)) {
        String _layoutName = this.extensions.layoutName(layout);
        String _operator_plus = StringExtensions.operator_plus(_layoutName, ".xml");
        StringConcatenation _generate = this.generate(layout);
        fsa.generateFile(_operator_plus, IGeneratorSlots.XML_SLOT, _generate);
      } else {
        String _layoutName_1 = this.extensions.layoutName(layout);
        String _operator_plus_1 = StringExtensions.operator_plus(_layoutName_1, ".xml");
        StringConcatenation _generate_1 = this.generate(layout);
        fsa.generateFile(_operator_plus_1, IGeneratorSlots.LAYOUT_SLOT, _generate_1);
      }
    }
  }
  
  public StringConcatenation generate(final RootLayout gui) {
    StringConcatenation _builder = new StringConcatenation();
    StringConcatenation _xmlHeader = this.extensions.xmlHeader(gui);
    _builder.append(_xmlHeader, "");
    _builder.newLineIfNotEmpty();
    _builder.append("<");
    EClass _eClass = gui.eClass();
    String _name = _eClass.getName();
    _builder.append(_name, "");
    _builder.append(" ");
    StringConcatenation _androidSchema = this.extensions.androidSchema(gui);
    _builder.append(_androidSchema, "");
    _builder.append(" ");
    StringConcatenation _attributes = this.attributes(gui);
    _builder.append(_attributes, "");
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    _builder.append("\t");
    StringConcatenation _generateElements = this.generateElements(gui);
    _builder.append(_generateElements, "	");
    _builder.newLineIfNotEmpty();
    _builder.append("</");
    EClass _eClass_1 = gui.eClass();
    String _name_1 = _eClass_1.getName();
    _builder.append(_name_1, "");
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public StringConcatenation uielement(final UIElement element) {
    StringConcatenation _builder = new StringConcatenation();
    StringConcatenation _startTag = this.startTag(element);
    _builder.append(_startTag, "");
    _builder.newLineIfNotEmpty();
    StringConcatenation _endTag = this.endTag(element);
    _builder.append(_endTag, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public StringConcatenation startTag(final UIElement element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("<");
    EClass _eClass = element.eClass();
    String _name = _eClass.getName();
    _builder.append(_name, "");
    _builder.append(" ");
    StringConcatenation _attributes = this.attributes(element);
    _builder.append(_attributes, "");
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected StringConcatenation _attributes(final UIElement layout) {
    StringConcatenation _builder = new StringConcatenation();
    return _builder;
  }
  
  protected StringConcatenation _attributes(final Layout layout) {
    StringConcatenation _builder = new StringConcatenation();
    return _builder;
  }
  
  protected StringConcatenation _attributes(final RootLayout layout) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:id=\"@+id/");
    String _name = layout.getName();
    _builder.append(_name, "");
    _builder.append("\" ");
    _builder.newLineIfNotEmpty();
    _builder.append("android:name=\"");
    String _name_1 = layout.getName();
    _builder.append(_name_1, "");
    _builder.append("\"");
    _builder.newLineIfNotEmpty();
    StringConcatenation _layoutAttributes = this.layoutAttributes(layout);
    _builder.append(_layoutAttributes, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected StringConcatenation _attributes(final Widget widget) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:id=\"@+id/");
    String _name = widget.getName();
    _builder.append(_name, "");
    _builder.append("\"");
    _builder.newLineIfNotEmpty();
    _builder.append("android:name=\"");
    String _name_1 = widget.getName();
    _builder.append(_name_1, "");
    _builder.append("\"");
    _builder.newLineIfNotEmpty();
    StringConcatenation _layoutAttributes = this.layoutAttributes(widget);
    _builder.append(_layoutAttributes, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected StringConcatenation _layoutAttributes(final BaseLayout layout) {
    StringConcatenation _builder = new StringConcatenation();
    LayoutStyle _layoutStyle = layout.getLayoutStyle();
    StringConcatenation _layoutParams = this.layoutParams(_layoutStyle);
    _builder.append(_layoutParams, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected StringConcatenation _layoutAttributes(final Widget widget) {
    StringConcatenation _builder = new StringConcatenation();
    LayoutStyle _layoutStyle = widget.getLayoutStyle();
    StringConcatenation _layoutParams = this.layoutParams(_layoutStyle);
    _builder.append(_layoutParams, "");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  public StringConcatenation layoutParams(final LayoutStyle style) {
    StringConcatenation _switchResult = null;
    final LayoutStyle style_1 = style;
    boolean matched = false;
    if (!matched) {
      if (ObjectExtensions.operator_equals(style_1,LayoutStyle.FILL)) {
        matched=true;
        StringConcatenation _fillLayout = this.fillLayout();
        _switchResult = _fillLayout;
      }
    }
    if (!matched) {
      if (ObjectExtensions.operator_equals(style_1,LayoutStyle.WRAP)) {
        matched=true;
        StringConcatenation _wrapLayout = this.wrapLayout();
        _switchResult = _wrapLayout;
      }
    }
    if (!matched) {
      if (ObjectExtensions.operator_equals(style_1,LayoutStyle.FILL_WRAP)) {
        matched=true;
        StringConcatenation _fillwrapLayout = this.fillwrapLayout();
        _switchResult = _fillwrapLayout;
      }
    }
    if (!matched) {
      if (ObjectExtensions.operator_equals(style_1,LayoutStyle.WRAP_FILL)) {
        matched=true;
        StringConcatenation _wrapfillLayout = this.wrapfillLayout();
        _switchResult = _wrapfillLayout;
      }
    }
    return _switchResult;
  }
  
  public StringConcatenation fillLayout() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:layout_width=\"fill_parent\"");
    _builder.newLine();
    _builder.append("android:layout_height=\"fill_parent\"");
    _builder.newLine();
    return _builder;
  }
  
  public StringConcatenation wrapLayout() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:layout_width=\"wrap_content\"");
    _builder.newLine();
    _builder.append("android:layout_height=\"wrap_content\"");
    _builder.newLine();
    return _builder;
  }
  
  public StringConcatenation fillwrapLayout() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:layout_width=\"fill_parent\"");
    _builder.newLine();
    _builder.append("android:layout_height=\"wrap_content\"");
    _builder.newLine();
    return _builder;
  }
  
  public StringConcatenation wrapfillLayout() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("android:layout_width=\"wrap_content\"");
    _builder.newLine();
    _builder.append("android:layout_height=\"fill_parent\"");
    _builder.newLine();
    return _builder;
  }
  
  public StringConcatenation endTag(final UIElement element) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("</");
    EClass _eClass = element.eClass();
    String _name = _eClass.getName();
    _builder.append(_name, "");
    _builder.append(">");
    _builder.newLineIfNotEmpty();
    return _builder;
  }
  
  protected StringConcatenation _generateElements(final Layout layout) {
    StringConcatenation _builder = new StringConcatenation();
    return _builder;
  }
  
  protected StringConcatenation _generateElements(final BaseLayout layout) {
    StringConcatenation _builder = new StringConcatenation();
    {
      EList<UIElement> _elements = layout.getElements();
      for(final UIElement e : _elements) {
        StringConcatenation _uielement = this.uielement(e);
        _builder.append(_uielement, "");
        _builder.newLineIfNotEmpty();
      }
    }
    return _builder;
  }
  
  protected StringConcatenation _generateElements(final PreferenceScreen prefScreen) {
    StringConcatenation _builder = new StringConcatenation();
    return _builder;
  }
  
  public StringConcatenation attributes(final UIElement layout) {
    if (layout instanceof RootLayout) {
      return _attributes((RootLayout)layout);
    } else if (layout instanceof Layout) {
      return _attributes((Layout)layout);
    } else if (layout instanceof Widget) {
      return _attributes((Widget)layout);
    } else {
      return _attributes(layout);
    }
  }
  
  public StringConcatenation layoutAttributes(final UIElement layout) {
    if (layout instanceof BaseLayout) {
      return _layoutAttributes((BaseLayout)layout);
    } else if (layout instanceof Widget) {
      return _layoutAttributes((Widget)layout);
    } else {
      throw new IllegalArgumentException("Unhandled parameter types: " +
        Arrays.<Object>asList(layout).toString());
    }
  }
  
  public StringConcatenation generateElements(final Layout layout) {
    if (layout instanceof BaseLayout) {
      return _generateElements((BaseLayout)layout);
    } else if (layout instanceof PreferenceScreen) {
      return _generateElements((PreferenceScreen)layout);
    } else {
      return _generateElements(layout);
    }
  }
}
